This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout

R 交互式数据可视化包 “plotly”

名称:通过‘plotly.js’ 来制作交互式网络图表
版本:4.9.0
版权:MIT + 文件 LICENSE
介绍:从ggplot2制作交互式图标和/或者运用图标语言标准(grammer of graphics) 新定制一个连接界面接口的(MIT版权所有的)Javascript包’plotly.js’
网站:https://plotly-r.com, https://github.com/ropensci/plotly#readme, https://plot.ly/r
修复历史文件:https://github.com/ropensci/plotly/issues
所需运行环境:R(版本>= 3.2.0),ggplot2(>= 3.0.0)
嵌入包:tools, scales, httr, jsonlite (>= 1.6), magrittr, digest, viridisLite, base64enc, htmltools (>= 0.3.6), htmlwidgets (>= 1.3), tidyr, hexbin, RColorBrewer, dplyr, tibble, lazyeval (>= 0.2.0), rlang, crosstalk, purrr, data.table, promises
建议搭配包:MASS, maps, ggthemes, GGally, testthat, knitr, devtools, shiny (>= 1.1.0), shinytest (>= 1.3.0), curl, rmarkdown, vdiffr, Cairo, broom, webshot, listviewer, dendextend, sf, maptools, rgeos, png, IRdisplay, processx, plotlyGeoAssets, forcats
惰性存储数据功能:开启
Roxygen版本:6.1.1
解码标准:UTF-8
是否需要编制:不需要
作者:Carson Sievert [aut, cre] (https://orcid.org/0000-0002-4958-2844), Chris Parmer [aut], Toby Hocking [aut], Scott Chamberlain [aut], Karthik Ram [aut], Marianne Corvellec [aut] (https://orcid.org/0000-0002-1994-3581), Pedro Despouy [aut], Plotly Technologies Inc. [cph]
维护者:Carson Sievert cpsievert1@gmail.com
文件地点:CRAN
发布时间:2019-04-10 19:33:05 UTC

R 主题/函数目录:


add_annotations:


描述:给图表加备注
用法:add_annotations(p, text = NULL, …, data = NULL, inherit = TRUE)
参数:
p:一个交互式图表
text:需要加上的备注(必须加上的)
…:这些参数被记录在此网站https://github.com/plotly/plotly.js/ blob/master/src/components/annotations/attributes.js
data: 一个数据表格(DataFrame格式)
inherit:是否要沿袭plot_ly()里面设置的属性?
作者:Carson Sievert


add_data:


描述:给图表加数据
用法:add_data(p, data = NULL)
参数:
p:一个交互式图表
data:一个Dataframe格式的数据表格
例子:

plot_ly() %>% add_data(economics) %>% add_trace(x = ~date, y = ~pce)
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode

add_fun:


描述:当需要加多层数据信息总结的时候可以用此函数
用法:add_fun(p, fun, …)
参数:
p:一个交互式图表
fun:一个函数,可以用来根据输入的图表为基础并且输出一个新的图表
…:给以上函数fun的参数


add_trace:


描述:给图表加各种图表标记
用法:
add_trace(p, …, data = NULL, inherit = TRUE) add_markers(p, x = NULL, y = NULL, z = NULL, …, data = NULL, inherit = TRUE) add_text(p, x = NULL, y = NULL, z = NULL, text = NULL, …, data = NULL, inherit = TRUE) add_paths(p, x = NULL, y = NULL, z = NULL, …, data = NULL, inherit = TRUE) add_lines(p, x = NULL, y = NULL, z = NULL, …, data = NULL, inherit = TRUE) add_segments(p, x = NULL, y = NULL, xend = NULL, yend = NULL, …, data = NULL, inherit = TRUE) add_polygons(p, x = NULL, y = NULL, …, data = NULL, inherit = TRUE) add_sf(p, …, x = ~x, y = ~y, data = NULL, inherit = TRUE) add_table(p, …, rownames = TRUE, data = NULL, inherit = TRUE) add_ribbons(p, x = NULL, ymin = NULL, ymax = NULL, …, data = NULL, inherit = TRUE) add_area(p, r = NULL, t = NULL, …, data = NULL, inherit = TRUE) add_pie(p, values = NULL, labels = NULL, …, data = NULL, inherit = TRUE) add_bars(p, x = NULL, y = NULL, …, data = NULL, inherit = TRUE) add_histogram(p, x = NULL, y = NULL, …, data = NULL, inherit = TRUE) add_histogram2d(p, x = NULL, y = NULL, z = NULL, …, data = NULL, inherit = TRUE) add_histogram2dcontour(p, x = NULL, y = NULL, z = NULL, …, data = NULL, inherit = TRUE) add_heatmap(p, x = NULL, y = NULL, z = NULL, …, data = NULL, inherit = TRUE) add_contour(p, z = NULL, …, data = NULL, inherit = TRUE) add_boxplot(p, x = NULL, y = NULL, …, data = NULL, inherit = TRUE) add_surface(p, z = NULL, …, data = NULL, inherit = TRUE) add_mesh(p, x = NULL, y = NULL, z = NULL, …, data = NULL, inherit = TRUE) add_scattergeo(p, …) add_choropleth(p, z = NULL, …, data = NULL, inherit = TRUE)
参数:
p:一个交互式图表
…:这些属性是赋予trace type的。想要知道所有的属性可以看schema(),在trace -> type -> attributes 里面可以找到
data:一个DataFrame格式的表格或者crosstalk包里的sharedData表格
inherit:是否要沿袭plot_ly()里设置的属性?
x:x坐标的取值(开始x的值)
y:y坐标的取值(开始y的值)
z:一个数字的矩阵
text:文字标签
xend:最终x的位置
yend:最终y的位置
rownames:是否要展示每行的名字
ymin:一个变量用来规定多边形的底部取值
ymax:一个标量用来规定多边形的顶部取值
r:只有极坐标需要此参数
t:只有极坐标需要此参数
values:每个圆形分格表的分格大小
labels:每个圆形分格表的分格标注


animation_opts:


描述:可用plot_ly()里面的frame参数实现动画化,或者(非正式)的ggplot2里面的美学设置来实现动画化。默认情况下,动画会配有一个播放按钮,以及一个进度条来控制播放进度。播放按钮和进度条都是根据animation_opts()里设置的参数来运作的
用法:animation_opts(p, frame = 500, transition = frame, easing = “linear”, redraw = TRUE, mode = “immediate”) animation_slider(p, hide = FALSE, …) animation_button(p, …, label)
参数:
p:一个互动式图表
frame:两帧之间停留的时间(包括过度时间)
transition:两帧之间过度时间
easing:过度类别,可以在https://github.com/ plotly/plotly.js/blob/master/src/plots/animation_attributes.js中选择
redraw:是否在过度之后需要重新绘图?重新绘图可能会很大程度上影响速度,但有时候是必要的
made:描述想要新的动画模式怎么和已有的动画兼容
hide:是否要移除进度条?
label:给动画播放按钮的标注
作者:Carson Sievert
例子:

df <- data.frame(
x = c(1, 2, 2, 1, 1, 2),
y = c(1, 2, 2, 1, 1, 2),
z = c(1, 1, 2, 2, 3, 3)
)
plot_ly(df) %>%
add_markers(x = 1.5, y = 1.5) %>%
add_markers(x = ~x, y = ~y, frame = ~z)
# it's a good idea to remove smooth transitions when there is
# no relationship between objects in each view
plot_ly(mtcars, x = ~wt, y = ~mpg, frame = ~cyl) %>%
animation_opts(transition = 0)
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
# works the same way with ggplotly
if (interactive()) {
p <- ggplot(txhousing, aes(month, median)) +
geom_line(aes(group = year), alpha = 0.3) +
geom_smooth() +
geom_line(aes(frame = year, ids = month), color = "red") +
facet_wrap(~ city)
ggplotly(p, width = 1200, height = 900) %>%
animation_opts(1000)
}

colorbar:


描述:改变图表的颜色条
用法:colorbar(p, …, limits = NULL, which = 1)
参数:
p:一个交互式图表
…:这些参数可以在这里找到https://plot.ly/r/reference/#scatter-marker-colorbar.
limits:一个长度为2的数字向量
which:表明哪一个是想要更改的颜色条
作者:Carson Sievert
例子:

p <- plot_ly(mtcars, x = ~wt, y = ~mpg, color = ~cyl)
# pass any colorbar attribute --
# https://plot.ly/r/reference/#scatter-marker-colorbar
colorbar(p, len = 0.5)
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
# Expand the limits of the colorbar
colorbar(p, limits = c(0, 20))
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
# values outside the colorbar limits are considered "missing"
colorbar(p, limits = c(5, 6))
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
# also works on colorbars generated via a z value
corr <- cor(diamonds[vapply(diamonds, is.numeric, logical(1))])
plot_ly(x = rownames(corr), y = colnames(corr), z = corr) %>%
add_heatmap() %>%
colorbar(limits = c(-1, 1))

embed_notebook:


描述:把图表嵌入python语言的编辑器里
用法:embed_notebook(x, width = NULL, height = NULL, file = NULL)
参数:
x:一个交互式图表
width:嵌入图表的宽度。如果是NULL,则沿袭x的宽度;如果不是NULL,则采用系统默认为100%
height:嵌入图标的高度。如果是NULL,则沿袭x的高度;如果不是NULL,则采用系统默认为400px
file:已被移除
作者:Carson Sievert


ggplotly:


描述:把ggplot2做的ggplot()图表转换成交互式图表
用法:ggplotly(p = ggplot2::last_plot(), width = NULL, height = NULL, tooltip = “all”, dynamicTicks = FALSE, layerData = 1, originalData = TRUE, source = “A”, …)
参数:
p:一个交互式图表 width:图表的宽度
height:图表的高度
tooltip:一个用来表明用哪种美学函数的向量。向量里的顺序会决定图标里变量的顺序
dynamicTicks:决定是否要动态的输出坐标轴标注
layerData:决定要返回输出哪一层的图像
originalData:决定应该输出原始数据还是标准化过的数据
source:一个长度为1字符。可以用event_data()里的参数来提取里面特定的事件信息
详细信息:转换的大小会根据正在运行的图表设备来决定(如果没有正在运行的设备,系统默认值是640/480)。换句话来说,高度和宽度一定要在运行程序的时候设定来保证大小的正确性
作者:Carson Sievert
借鉴网站:https://plot.ly/ggplot2
以及:plot_ly()
例子:

# simple example
ggiris <- qplot(Petal.Width, Sepal.Length, data = iris, color = Species)
ggplotly(ggiris)
# linked scatterplot brushing
d <- highlight_key(mtcars)
qplot(data = d, x = mpg, y = wt) %>%
subplot(qplot(data = d, x = mpg, y = vs)) %>%
layout(title = "Click and drag to select points") %>%
highlight("plotly_selected")
## Setting the `off` event (i.e., 'plotly_deselect') to match the `on` event (i.e., 'plotly_selected'). You can change this default via the `highlight()` function.
# more brushing (i.e. highlighting) examples
demo("crosstalk-highlight-ggplotly", package = "plotly")
## 
## 
##  demo(crosstalk-highlight-ggplotly)
##  ---- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## 
## > library(plotly)
## 
## > # see https://vimeo.com/202647310
## > d <- highlight_key(txhousing, ~city, "Select a city")
## 
## > p <- ggplot(d, aes(date, median, group = city)) + geom_line()
## 
## > ggplotly(p, tooltip = "city") %>%
## +   layout(title = "Click on a line to highlight a year") %>%
## +   highlight(dynamic = TRUE, selectize = TRUE)
## Adding more colors to the selection color palette.
## Setting the `off` event (i.e., 'plotly_doubleclick') to match the `on` event (i.e., 'plotly_click'). You can change this default via the `highlight()` function.
## 
## > # crosstalk keys are automatically added to the group aesthetic...
## > # if you want to avoid adding the key to group for a layer,
## > # use the original data
## > p <- ggplot(d, aes(month, median)) +
## +   geom_line(aes(group = city)) + 
## +   geom_smooth(data = txhousing, method = "gam") + 
## +   facet_wrap(~ year)
## 
## > ggplotly(p) %>%
## +   layout(title = "Click on a line to highlight a year")
## Warning: Removed 616 rows containing non-finite values (stat_smooth).
## 
## > # perhaps a more useful example
## > sd <- highlight_key(txhousing, ~year)
## 
## > p <- ggplot(sd, aes(month, median)) +
## +   geom_line(aes(group = year)) + 
## +   geom_smooth(data = txhousing, method = "gam") + 
## +   facet_wrap(~ city)
## 
## > ggplotly(p, height = 800, width = 1600) %>%
## +   layout(title = "Click on a line to highlight a year")
## Warning: Removed 616 rows containing non-finite values (stat_smooth).
# client-side linked brushing in a scatterplot matrix
highlight_key(iris) %>%
GGally::ggpairs(aes(colour = Species), columns = 1:4) %>%
ggplotly(tooltip = c("x", "y", "colour")) %>%
highlight("plotly_selected")
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
## Warning: Can only have one: highlight
## Warning: Can only have one: highlight

## Warning: Can only have one: highlight
## Setting the `off` event (i.e., 'plotly_deselect') to match the `on` event (i.e., 'plotly_selected'). You can change this default via the `highlight()` function.

group2NA:


描述:这个函数主要是plotly内部调试用的,但也对熟练的用户有帮助
用法:group2NA(data, groupNames = “group”, nested = NULL, ordered = NULL, retrace.first = inherits(data, “GeomPolygon”))
参数:
data:一个DataFrame格式的数据表格
groupName:一个用来分组的向量
nested:其他的分组函数
ordered:一个用来排序的向量
retrace.first:是否将每组的最后一行和组的第一行贴在一起?
详细信息:如果一个组的分布趋势有共同的非位置特性(例如颜色等等),把他们用同一个特性整合在一起,然后用每组缺失的数据来区分效率会更高(要注意connectgaps是设定成False的)
价值:一个表格首先按照行来排序,然后每组的名字,最后再排序。只要每组名字有正确的变量的名字,新的行会被正确输入来区分各个组
例子:

# note the insertion of new rows with missing values
group2NA(mtcars, "vs", "cyl")
##     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## 1  26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
## 2    NA   4    NA  NA   NA    NA    NA  0 NA   NA   NA
## 3  22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
## 4  24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## 5  22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## 6  32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
## 7  30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
## 8  33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
## 9  21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
## 10 27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## 11 30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## 12 21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
## 13 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## 14 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## 15 19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
## 16   NA   6    NA  NA   NA    NA    NA  0 NA   NA   NA
## 17 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## 18 18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
## 19 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
## 20 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
## 21 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
## 22 14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
## 23 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
## 24 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
## 25 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
## 26 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
## 27 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
## 28 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
## 29 15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
## 30 15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
## 31 13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
## 32 19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
## 33 15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
## 34 15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
# need to group lines by city somehow!
plot_ly(txhousing, x = ~date, y = ~median) %>% add_lines()
# instead of using group_by(), you could use group2NA()
tx <- group2NA(txhousing, "city")
plot_ly(tx, x = ~date, y = ~median) %>% add_lines()
# add_lines() will ensure paths are sorted by x, but this is equivalent
tx <- group2NA(txhousing, "city", ordered = "date")
plot_ly(tx, x = ~date, y = ~median) %>% add_paths()

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.